package com.botarena.server.dao;

import java.util.Collection;
import java.util.LinkedList;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.botarena.server.model.Battle;
import com.google.appengine.api.datastore.Key;

public class BattleDAO extends GenericDAO<Battle> {
	
	public BattleDAO(PersistenceManager pm) {
		super(pm);
	}
	
	@SuppressWarnings("unchecked")
	public Collection<Battle> getLastest(Key contestKey, int limit) {
		Query query = pm.newQuery(getPersistentClass());
		query.setFilter("contest == chosenContest");
		query.setOrdering("time desc");
		query.setRange(0, limit);
		query.declareImports("import com.google.appengine.api.datastore.Key");
		query.declareParameters("Key chosenContest");
	    
	    return (Collection<Battle>) query.execute(contestKey);
	}
	
	@SuppressWarnings("unchecked")
	public Collection<Battle> getBotFights(Key botKey, int limit) {		
		Query query = pm.newQuery(getPersistentClass());
		query.setFilter("bot1 == chosenBot");
		query.setOrdering("time desc");
		query.setRange(0, limit);
		query.declareImports("import com.google.appengine.api.datastore.Key");
		query.declareParameters("Key chosenBot");
		
		Query query2 = pm.newQuery(getPersistentClass());
		query2.setFilter("bot2 == chosenBot");
		query2.setOrdering("time desc");
		query2.setRange(0, limit);
		query2.declareImports("import com.google.appengine.api.datastore.Key");
		query2.declareParameters("Key chosenBot");
	    
		Collection<Battle> all = new LinkedList<Battle>();
		all.addAll((Collection<Battle>) query.execute(botKey));
		all.addAll((Collection<Battle>) query2.execute(botKey));
		
	    return all;
	}

	public int getBotWins(final Key bot, final Key contest) {
		Query query = pm.newQuery(getPersistentClass());
		query.setResult("count(key)");
		query.setFilter("winner == bot && contest == chosenContest");
		query.declareImports("import com.google.appengine.api.datastore.Key");
		query.declareParameters("Key bot, Key chosenContest");
		
		return ((Integer)query.execute(bot, contest)).intValue();
	}
	
	public int getBotLosses(final Key bot, final Key contest) {
		// query has to be splitted cause AppEngine only supports '||' filter on equality expressions on the same field.. :(
		// so query (bot1 == bot || bot2 == bot) won't work -> UnsupportedDatastoreException
		Query query = pm.newQuery(getPersistentClass());
		query.setResult("count(key)");
		query.setFilter("bot1 == bot && winner != bot && contest == chosenContest");
		query.declareImports("import com.google.appengine.api.datastore.Key");
		query.declareParameters("Key bot, Key chosenContest");
		
		Query query2 = pm.newQuery(getPersistentClass());
		query2.setResult("count(key)");
		query2.setFilter("bot2 == bot && winner != bot && contest == chosenContest");
		query2.declareImports("import com.google.appengine.api.datastore.Key");
		query2.declareParameters("Key bot, Key chosenContest");
		
		return ((Integer)query.execute(bot, contest)).intValue() + ((Integer)query2.execute(bot, contest)).intValue();
	}
}
